package com.woniu.adopt.web;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.Manager;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.ManagerService;
import com.woniu.adopt.utils.JwtUtil;
import com.woniu.adopt.vo.AccountVo;
import com.woniu.adopt.vo.JwtInfo;
import com.woniu.adopt.vo.ManagerVo;
import com.woniu.adopt.vo.TeamVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("manager")
@Api(description = "后台用户管理接口")
public class ManagerController {

    @Autowired
    ManagerService managerService;

    @ApiOperation(value = "后台用户登录功能")
    @PostMapping("/login")
    public ResultResponse<Manager> login(
            @ApiParam(name = "accountVo", value = "接收后台登录账号和密码")
            @RequestBody AccountVo accountVo) {
        ResultResponse<Manager> result = null;
        try {
            Manager manager = managerService.login(accountVo.getUserName());
            if (manager == null) {
                result = new ResultResponse<>(201, "账号不存在!");
            } else if (manager.getLocked() == 1) {
                result = new ResultResponse<>(202, "账号被锁定，无法登陆!");
            } else if (!manager.getPassword().equals(accountVo.getUserPassword())) {
                result = new ResultResponse<>(203, "密码错误!");
            } else if (manager.getPassword().equals(accountVo.getUserPassword())) {
                JwtInfo jwtInfo = new JwtInfo();
                jwtInfo.setUserName(manager.getUsername());
                String token = JwtUtil.createToken(jwtInfo);
                result = new ResultResponse<>(200, "登录成功", manager, token);
            } else {
                result = new ResultResponse<>(205, "未知错误!");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(204, "系统错误，请联系管理员!");
        }
        return result;
    }

    @ApiOperation(value = "后台用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "now", value = "查询的当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "size", value = "每页显示多少条", required = true, defaultValue = "3"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "管理员的昵称", required = false)
    })
    @GetMapping("/list")
    public ResultResponse<PageInfo<ManagerVo>> rlist(@RequestParam(value = "now", defaultValue = "1") Integer now,
                                                     @RequestParam(value = "size", defaultValue = "3") Integer size,
                                                     String name) {
        ResultResponse<PageInfo<ManagerVo>> result = null;

        try {
            PageInfo<ManagerVo> rolePageInfo = managerService.queryByPage(now, size, name);
            result = new ResultResponse<>(200, "查询成功!", rolePageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败!");
        }
        return result;
    }

    @ApiOperation(value = "添加管理员")
    @PostMapping("/add")
    public ResultResponse<Void> add(
            @ApiParam(name = "managerVo", value = "接收添加管理员表单信息")
            @RequestBody ManagerVo managerVo) {
        ResultResponse<Void> result = null;
        try {
            managerVo.setInsertTime(new Date());
            managerService.add(managerVo);
            result = new ResultResponse<>(200, "添加成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加失败!");
        }
        return result;
    }

    @ApiOperation(value = "修改用户")
    @PutMapping("/edit")
    public ResultResponse<Void> edit(
            @ApiParam(name = "managerVo", value = "接收修改管理员表单信息")
            @RequestBody ManagerVo managerVo) {
        ResultResponse<Void> result = null;
        try {
            managerVo.setUpdateTime(new Date());
            managerService.modify(managerVo);
            result = new ResultResponse<>(200, "修改成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改失败!");
        }
        return result;
    }

    @ApiOperation(value = "根据用户ID查询用户角色明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员id", required = true)
    })
    @GetMapping("/getRoleNamesByMid/{id}")
    public ResultResponse<List<String>> getRoleNamesByMid(@PathVariable("id") int mid) {
        ResultResponse<List<String>> result = null;

        try {
            List<String> names = managerService.getRoleNamesByMid(mid);
            result = new ResultResponse<>(200, "查询成功!", names);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败!");
        }
        return result;
    }

    @ApiOperation(value = "根据用户ID删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员id", required = true)
    })
    @DeleteMapping("/delete/{id}")
    public ResultResponse<Void> del(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            managerService.removeByKey(id);
            result = new ResultResponse<>(200, "删除成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(202, "后端写错了");
        }
        return result;
    }

    @ApiOperation(value = "根据用户ID切换锁定状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "管理员id", required = true)
    })
    @PutMapping("/changeLocked/{id}")
    public ResultResponse<Void> changeLocked(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            managerService.changeLocked(id);
            result = new ResultResponse<>(200, "状态切换成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(202, "后端写错了");
        }
        return result;
    }

    //-----------------------------------------------团队信息操作部分-------------------------------------------------------------//

    @ApiOperation(value = "我的团队相关信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "now", value = "查询的当前页", required = false, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "size", value = "每页显示多少条", required = false, defaultValue = "3"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "管理员的用户名", required = false)
    })
    @GetMapping("/teamList")
    public ResultResponse<PageInfo<TeamVo>> tlist(@RequestParam(value = "now", defaultValue = "1") Integer now,
                                                  @RequestParam(value = "size", defaultValue = "3") Integer size,
                                                  String name) {
        ResultResponse<PageInfo<TeamVo>> result = null;

        try {
            PageInfo<TeamVo> teamPageInfo = managerService.queryTeamByPage(now, size, name);
            result = new ResultResponse<>(200, "查询成功!", teamPageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败!");
        }
        return result;
    }

    @ApiOperation(value = "添加团队信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pic", value = "图片", required = true),
            @ApiImplicitParam(name = "introduce", value = "个人介绍"),
            @ApiImplicitParam(name = "message", value = "养宠寄语"),
    })
    @PostMapping("/teamAdd")
    public ResultResponse<Void> teamAdd(@RequestBody TeamVo teamVo) {
        ResultResponse<Void> result = null;
        try {
            managerService.addTeam(teamVo);
            result = new ResultResponse<>(200, "添加成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加失败!");
        }
        return result;
    }

    @PostMapping("/teamEdit")
    @ApiOperation("团队信息修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pic", value = "图片", required = true),
            @ApiImplicitParam(name = "introduce", value = "个人介绍"),
            @ApiImplicitParam(name = "message", value = "养宠寄语"),
    })
    public ResultResponse<Void> editTeam(@RequestBody TeamVo teamVo) {
        ResultResponse<Void> result = null;
        try {
            managerService.modifyTeam(teamVo);
            result = new ResultResponse<>(200, "修改成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改失败!");
        }
        return result;
    }

    @ApiOperation(value = "根据用户ID删除团队信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true)
    })
    @DeleteMapping("/teamDelete/{id}")
    public ResultResponse<Void> teamDelete(@PathVariable("id") int id) {
        Integer.parseInt("abd");
        ResultResponse<Void> result = null;
        try {
            managerService.removeTeamByKey(id);
            result = new ResultResponse<>(200, "删除成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(202, "后端写错了");
        }
        return result;
    }

    //----------------------------------------------历史团队操作-------------------------------------------------//
    @ApiOperation(value = "我的团队已删信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "now", value = "查询的当前页", required = false, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "size", value = "每页显示多少条", required = false, defaultValue = "3"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "管理员的用户名", required = false)
    })
    @GetMapping("/teamListOld")
    public ResultResponse<PageInfo<TeamVo>> teamListOld(@RequestParam(value = "now", defaultValue = "1") Integer now,
                                                        @RequestParam(value = "size", defaultValue = "3") Integer size,
                                                        String name) {
        ResultResponse<PageInfo<TeamVo>> result = null;

        try {
            PageInfo<TeamVo> teamPageInfo = managerService.queryTeamByPageOld(now, size, name);
            result = new ResultResponse<>(200, "查询成功!", teamPageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败!");
        }
        return result;
    }

    @ApiOperation(value = "根据用户ID追回团队信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true)
    })
    @PostMapping("/teamRecycle/{id}")
    public ResultResponse<Void> teamRecycle(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            managerService.recycleTeamByKey(id);
            result = new ResultResponse<>(200, "回收成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(202, "后端写错了");
        }
        return result;
    }

    @ApiOperation(value = "新增管理员时的重名验证")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "userName", value = "管理员的用户名", required = false)
    })
    @GetMapping("/judgedSameName")
    public ResultResponse<Void> judgedSameName(String userName) {
        ResultResponse<Void> result = null;

        try {
            int i = managerService.judgedSameName(userName);
            String msg="用户名："+userName+"已被注册，请更换您的用户名";
            if(i!=0){
                result = new ResultResponse<>(201, msg);
            }else {
                result = new ResultResponse<>(200, "该用户名可以使用!");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(202, "系统错误!");
        }
        return result;
    }
}
